package controller.goods;

import entity.Goods;
import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;

public class Goods_update extends JDialog implements ActionListener {
    JPanel p1,p2;
    private JComboBox sort_Box; //”类别选择“
    private HashMap hashMap = new HashMap();
    private Vector<String> vector = new Vector<String>();   //类别数组,同步性安全

    //单行文本框组件
    private JTextField[] textFields;
    JButton btn_ok,btn_cancel;

    public Goods_update(Goods goods) {
        this.setTitle("物品修改");
        this.setSize(400,350);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setModal(true);    //模态窗口
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());

        Box boxH; // 行式盒
        Box boxVOne, boxVTwo; // 列式盒

        boxH = Box.createHorizontalBox();   //水平
        boxVOne = Box.createVerticalBox();  //竖直
        boxVTwo = Box.createVerticalBox();
        boxVOne.add(Box.createVerticalStrut(50));
        boxVTwo.add(Box.createVerticalStrut(50));

        //JLabel标签组件
        JLabel[] labels = new JLabel[5];
        String[] name = {"货 号","类 别","货 物","价 格","数 量"};
        vector.add("");

        for (int i = 0; i < 5; i++) {
            labels[i] = new JLabel(name[i]);
            boxVOne.add(labels[i]);
            //固定高度的组件createVerticalStrut
            boxVOne.add(Box.createVerticalStrut(10));
        }

        textFields = new JTextField[5];
        for (int i = 0; i < 5; i++) {
            if(i == 1){
                try {
                    String sql = "SELECT * FROM sort";
                    JdbcDeal jdbcDeal = new JdbcDeal();
                    ResultSet resultSet = jdbcDeal.select(sql);
                    while (resultSet.next()){
                        vector.add(resultSet.getString("Sname"));
                        hashMap.put(resultSet.getString("Sname"),resultSet.getString("Sno"));
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                sort_Box = new JComboBox(vector);
                boxVTwo.add(sort_Box);
                continue;
            }
            textFields[i] = new JTextField(15);
            boxVTwo.add(textFields[i]);
        }
        //传值
        textFields[0].setText(goods.getGoodsNo());
        textFields[0].setEditable(false);
        textFields[2].setText(goods.getName());
        textFields[3].setText(goods.getPirce());
        textFields[4].setText(goods.getGnum());
        for (int j=0;j<vector.size();j++){
            if(goods.getGoodsort().equals(vector.get(j))){
                sort_Box.setSelectedIndex(j);
            }
        }

        boxH.add(boxVOne);
        //固定宽度的组件createHorizontalStrut
        boxH.add(Box.createHorizontalStrut(15));
        boxH.add(boxVTwo);
        p1.add(boxH);
        add(p1,BorderLayout.NORTH);

        boxH = Box.createHorizontalBox();
        boxVOne = Box.createVerticalBox();
        boxVTwo = Box.createVerticalBox();

        p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        btn_ok = new JButton("修改");
        btn_cancel = new JButton("取消");
        boxVOne.add(btn_ok);
        boxVOne.add(Box.createHorizontalStrut(25));
        boxVTwo.add(btn_cancel);
        boxVTwo.add(Box.createHorizontalStrut(25));
        boxH.add(boxVOne);
        boxH.add(boxVTwo);
        p2.add(boxH);
        add(p2,BorderLayout.CENTER);


        btn_ok.addActionListener(this);
        btn_cancel.addActionListener(this);
        this.setVisible(true);
        validate();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String sorts = sort_Box.getSelectedItem().toString();

        if(e.getSource() == btn_cancel){
            this.dispose();
            return;
        }

        //数据校验部分
        if(this.textFields[0].getText().equals("")){
            JOptionPane.showMessageDialog(this, "编号不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(sort_Box.getSelectedIndex() == 0){
            JOptionPane.showMessageDialog(this, "类别不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(this.textFields[2].getText().equals("")){
            JOptionPane.showMessageDialog(this, "姓名不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(this.textFields[3].getText().equals("")){
            JOptionPane.showMessageDialog(this, "价格不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(this.textFields[4].getText().equals("")){
            JOptionPane.showMessageDialog(this, "数量不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }


        //业务处理
        if(e.getSource() == btn_ok){

            JdbcDeal jdbcDeal = new JdbcDeal();
            String sql = "UPDATE goods SET Sno='"+(String) hashMap.get(sorts)+"',Gname='"+this.textFields[2].getText()+
                    "',Gpirce='"+this.textFields[3].getText()+"',Gnum='"+this.textFields[4].getText()+
                    "' WHERE Gno= '" + this.textFields[0].getText() + "'";

            try {
                if(jdbcDeal.Update(sql) == 0){
                    JOptionPane.showMessageDialog(this, "修改失败", "", JOptionPane.WARNING_MESSAGE);
                    jdbcDeal.close();
                    return ;
                }else{
                    JOptionPane.showMessageDialog(this,"修改成功","",JOptionPane.PLAIN_MESSAGE);
                    jdbcDeal.close();
                    this.dispose();
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
